package org.road0329.dlt698.axdr;

import org.road0329.dlt698.ReverseByteArrayInputStream;
import org.road0329.dlt698.ReverseByteArrayOutputStream;
import org.road0329.dlt698.asn1.axdr.AxdrEnum;


public class M698pdu {
    private Choice choice = Choice._ERR_NONE_SELECTED;
    private byte[] dataCode = null;
    private GetRequest getRequest = null;
    private SetRequest setRequest = null;
    private Action_Request actionRequest = null;
    private Security_Request securityRequest = null;
    private ConnectRequest connectRequest = null;
    private Proxy_Request proxyRequest = null;
    private GetResponse getResponse = null;
    private SetResponse setResponse = null;
    private Action_Response actionResponse = null;
    private Security_Response securityResponse = null;
    private ConnectResponse connectResponse = null;
    private Proxy_Response proxyResponse = null;

    private ReportNotification reportNotification = null;

    private static M698pdu instance = null;

    public static M698pdu getInstance() {
        if (instance == null) {
            instance = new M698pdu();
        }
        return instance;
    }

    public M698pdu() {
    }

    public void setDataCode(byte[] dataCode) {
        this.dataCode = dataCode;
    }

    public int decode(ReverseByteArrayInputStream input) throws Exception {
        int codeLength = 0;
        AxdrEnum choosen = new AxdrEnum();
        choosen.setConst();
        codeLength += choosen.decode(input);
        resetChoices();
        choice = Choice.getEnumByValue((int)choosen.getValue());
        if (choice == Choice.GET_REQUEST) {
            getRequest = new GetRequest();
            codeLength += getRequest.decode(input);
            return codeLength;
        } else if (choice == Choice.SET_REQUEST) {
            setRequest = new SetRequest();
            codeLength += setRequest.decode(input);
            return codeLength;
        } else if (choice == Choice.ACTION_REQUEST) {
            actionRequest = new Action_Request();
            codeLength += actionRequest.decode(input);
            return codeLength;
        } else if (choice == Choice.SECURITY_REQUEST) {
            securityRequest = new Security_Request();
            codeLength += securityRequest.decode(input);
            return codeLength;
        } else if (choice == Choice.CONNECT_REQUEST) {
            connectRequest = new ConnectRequest();
            codeLength += connectRequest.decode(input);
            return codeLength;
        } else if (choice == Choice.PROXY_REQUEST) {
            proxyRequest = new Proxy_Request();
            codeLength += proxyRequest.decode(input);
            return codeLength;
        } else if (choice == Choice.GET_RESPONSE) {
            getResponse = new GetResponse();
            codeLength += getResponse.decode(input);
            return codeLength;
        } else if (choice == Choice.SET_RESPONSE) {
            setResponse = new SetResponse();
            codeLength += setResponse.decode(input);
            return codeLength;
        } else if (choice == Choice.ACTION_RESPONSE) {
            actionResponse = new Action_Response();
            codeLength += actionResponse.decode(input);
            return codeLength;
        } else if (choice == Choice.SECURITY_RESPONSE) {
            securityResponse = new Security_Response();
            codeLength += securityResponse.decode(input);
            return codeLength;
        } else if (choice == Choice.CONNECT_RESPONSE) {
            connectResponse = new ConnectResponse();
            codeLength += connectResponse.decode(input);
            return codeLength;
        } else if (choice == Choice.PROXY_RESPONSE) {
            proxyResponse = new Proxy_Response();
            codeLength += proxyResponse.decode(input);
            return codeLength;
        } else if (choice == Choice.REPORT_NOTIFICATION) {
            reportNotification = new ReportNotification();
            codeLength += reportNotification.decode(input);
            return codeLength;
        }
        throw new Exception("Error decoding AxdrChoice: Identifier matched to no item.");
    }

    public int encode(ReverseByteArrayOutputStream output) throws Exception {
        if (dataCode != null) {
            for (int i = dataCode.length - 1; i >= 0; i--) {
                output.write(dataCode[i]);
            }
            return dataCode.length;
        }
        if (choice == Choice._ERR_NONE_SELECTED) {
            throw new Exception("Error encoding AxdrChoice: No item in choice was selected.");
        }
        int codeLength = 0;
        if (choice == Choice.GET_REQUEST) {
            if (getRequest != null) {
                codeLength += getRequest.encode(output);
                AxdrEnum c = new AxdrEnum();
                c.setMinMaxVal(5);
                codeLength += c.encode(output);
                return codeLength;
            } else {
                throw new Exception("get_request is null");
            }
        } else if (choice == Choice.SET_REQUEST) {
            if (setRequest != null) {
                codeLength += setRequest.encode(output);
                AxdrEnum c = new AxdrEnum();
                c.setMinMaxVal(6);
                codeLength += c.encode(output);
                return codeLength;
            } else {
                throw new Exception("set_request is null");
            }
        } else if (choice == Choice.ACTION_REQUEST) {
            if (actionRequest != null) {
                codeLength += actionRequest.encode(output);
                AxdrEnum c = new AxdrEnum();
                c.setMinMaxVal(7);
                codeLength += c.encode(output);
                return codeLength;
            } else {
                throw new Exception("action_request is null");
            }
        } else if (choice == Choice.SECURITY_REQUEST) {
            if (securityRequest != null) {
                codeLength += securityRequest.encode(output);
                AxdrEnum c = new AxdrEnum();
                c.setMinMaxVal(16);
                codeLength += c.encode(output);
                return codeLength;
            } else {
                throw new Exception("security_request is null");
            }
        } else if (choice == Choice.CONNECT_REQUEST) {
            if (connectRequest != null) {
                codeLength += connectRequest.encode(output);
                AxdrEnum c = new AxdrEnum();
                c.setMinMaxVal(2);
                codeLength += c.encode(output);
                return codeLength;
            } else {
                throw new Exception("connect_request is null");
            }
        } else if (choice == Choice.PROXY_REQUEST) {
            if (proxyRequest != null) {
                codeLength += proxyRequest.encode(output);
                AxdrEnum c = new AxdrEnum();
                c.setMinMaxVal(9);
                codeLength += c.encode(output);
                return codeLength;
            } else {
                throw new Exception("proxy_request is null");
            }
        } else if (choice == Choice.GET_RESPONSE) {
            if (getResponse != null) {
                codeLength += getResponse.encode(output);
                AxdrEnum c = new AxdrEnum();
                c.setMinMaxVal(133);
                codeLength += c.encode(output);
                return codeLength;
            } else {
                throw new Exception("get_response is null");
            }
        } else if (choice == Choice.SET_RESPONSE) {
            if (setResponse != null) {
                codeLength += setResponse.encode(output);
                AxdrEnum c = new AxdrEnum();
                c.setMinMaxVal(134);
                codeLength += c.encode(output);
                return codeLength;
            } else {
                throw new Exception("set_response is null");
            }
        } else if (choice == Choice.ACTION_RESPONSE) {
            if (actionResponse != null) {
                codeLength += actionResponse.encode(output);
                AxdrEnum c = new AxdrEnum();
                c.setMinMaxVal(135);
                codeLength += c.encode(output);
                return codeLength;
            } else {
                throw new Exception("action_response is null");
            }
        } else if (choice == Choice.SECURITY_RESPONSE) {
            if (securityResponse != null) {
                codeLength += securityResponse.encode(output);
                AxdrEnum c = new AxdrEnum();
                c.setMinMaxVal(144);
                codeLength += c.encode(output);
                return codeLength;
            } else {
                throw new Exception("security_response is null");
            }
        } else if (choice == Choice.CONNECT_RESPONSE) {
            if (connectResponse != null) {
                codeLength += connectResponse.encode(output);
                AxdrEnum c = new AxdrEnum();
                c.setMinMaxVal(130);
                codeLength += c.encode(output);
                return codeLength;
            } else {
                throw new Exception("connect_response is null");
            }
        } else if (choice == Choice.PROXY_RESPONSE) {
            if (proxyResponse != null) {
                codeLength += proxyResponse.encode(output);
                AxdrEnum c = new AxdrEnum();
                c.setMinMaxVal(137);
                codeLength += c.encode(output);
                return codeLength;
            } else {
                throw new Exception("proxy_response is null");
            }
        }
        throw new Exception("Error encoding AxdrChoice: No item in choice was encoded.");
    }

    public GetRequest getGetRequest() {
        return getRequest;
    }

    public SetRequest getSetRequest() {
        return setRequest;
    }

    public Action_Request getActionRequest() {
        return actionRequest;
    }

    public Security_Request getSecurityRequest() {
        return securityRequest;
    }

    public ConnectRequest getConnectRequest() {
        return connectRequest;
    }

    public Proxy_Request getProxyRequest() {
        return proxyRequest;
    }

    public GetResponse getGetResponse() {
        return getResponse;
    }

    public SetResponse getSetResponse() {
        return setResponse;
    }

    public Action_Response getActionResponse() {
        return actionResponse;
    }

    public Security_Response getSecurityResponse() {
        return securityResponse;
    }

    public ConnectResponse getConnectResponse() {
        return connectResponse;
    }

    public Proxy_Response getProxyResponse() {
        return proxyResponse;
    }

    public ReportNotification getReportNotification() {
        return reportNotification;
    }

    public void setGetRequest(GetRequest newVal) {
        resetChoices();
        choice = Choice.GET_REQUEST;
        getRequest = newVal;
    }

    public void setSetRequest(SetRequest newVal) {
        resetChoices();
        choice = Choice.SET_REQUEST;
        setRequest = newVal;
    }

    public void setActionRequest(Action_Request newVal) {
        resetChoices();
        choice = Choice.ACTION_REQUEST;
        actionRequest = newVal;
    }

    public void setSecurityRequest(Security_Request newVal) {
        resetChoices();
        choice = Choice.SECURITY_REQUEST;
        securityRequest = newVal;
    }

    public void setConnectRequest(ConnectRequest newVal) {
        resetChoices();
        choice = Choice.CONNECT_REQUEST;
        connectRequest = newVal;
    }

    public void setProxyRequest(Proxy_Request newVal) {
        resetChoices();
        choice = Choice.PROXY_REQUEST;
        proxyRequest = newVal;
    }

    public void setGetResponse(GetResponse newVal) {
        resetChoices();
        choice = Choice.GET_RESPONSE;
        getResponse = newVal;
    }

    public void setSetResponse(SetResponse newVal) {
        resetChoices();
        choice = Choice.SET_RESPONSE;
        setResponse = newVal;
    }

    public void setActionResponse(Action_Response newVal) {
        resetChoices();
        choice = Choice.SET_RESPONSE;
        actionResponse = newVal;
    }

    public void setSecurityResponse(Security_Response newVal) {
        resetChoices();
        choice = Choice.SECURITY_RESPONSE;
        securityResponse = newVal;
    }

    public void setProxyResponse(Proxy_Response newVal) {
        resetChoices();
        choice = Choice.PROXY_RESPONSE;
        proxyResponse = newVal;
    }

    public void setConnectResponse(ConnectResponse newVal) {
        resetChoices();
        choice = Choice.CONNECT_RESPONSE;
        connectResponse = newVal;
    }

    public Choice getChoiceIndex() {
        return choice;
    }

    public void resetChoices() {
        choice = Choice._ERR_NONE_SELECTED;
        getRequest = null;
        setRequest = null;
        actionRequest = null;
        securityRequest = null;
        connectRequest = null;
        proxyRequest = null;
        getResponse = null;
        setResponse = null;
        actionResponse = null;
        securityResponse = null;
        connectResponse = null;
        proxyResponse = null;
    }

    public String toString() {
        if (choice == Choice.GET_REQUEST) {
            if (getRequest != null) {
                return "choice: {get_Request: " + getRequest + "}";
            } else {
                return "choice is get_request but get_request is null";
            }
        } else if (choice == Choice.SET_REQUEST) {
            if (setRequest != null) {
                return "choice: {set_request: " + setRequest + "}";
            } else {
                return "choice is set_request but set_request is null";
            }
        } else if (choice == Choice.ACTION_REQUEST) {
            if (actionRequest != null) {
                return "choice: {action_request: " + actionRequest + "}";
            } else {
                return "choice is action_request but action_request is null";
            }
        } else if (choice == Choice.SECURITY_REQUEST) {
            if (securityRequest != null) {
                return "choice: {security_request: " + securityRequest + "}";
            } else {
                return "choice is security_request but security_request is null";
            }
        } else if (choice == Choice.CONNECT_REQUEST) {
            if (connectRequest != null) {
                return "choice: {connect_request: " + connectRequest + "}";
            } else {
                return "choice is connect_request but connect_request is null";
            }
        } else if (choice == Choice.PROXY_REQUEST) {
            if (proxyRequest != null) {
                return "choice: {proxy_request: " + proxyRequest + "}";
            } else {
                return "choice is proxy_request but proxy_request is null";
            }
        } else if (choice == Choice.GET_RESPONSE) {
            if (getResponse != null) {
                return "choice: {get_response: " + getResponse + "}";
            } else {
                return "choice is get_response but get_response is null";
            }
        } else if (choice == Choice.SET_RESPONSE) {
            if (setResponse != null) {
                return "choice: {set_response: " + setResponse + "}";
            } else {
                return "choice is set_response but set_response is null";
            }
        } else if (choice == Choice.ACTION_RESPONSE) {
            if (actionResponse != null) {
                return "choice: {action_response: " + actionResponse + "}";
            } else {
                return "choice is action_response but action_response is null";
            }
        } else if (choice == Choice.CONNECT_RESPONSE) {
            if (connectResponse != null) {
                return "choice: {connect_response: " + connectResponse + "}";
            } else {
                return "choice is connect_response but connect_response is null";
            }
        } else if (choice == Choice.PROXY_RESPONSE) {
            if (proxyResponse != null) {
                return "choice: {proxy_response: " + proxyResponse + "}";
            } else {
                return "choice is proxy_response but proxy_response is null";
            }
        } else if (choice == Choice.SECURITY_RESPONSE) {
            if (securityResponse != null) {
                return "choice: {security_response: " + securityResponse + "}";
            } else {
                return "choice is security_response but security_response is null";
            }
        } else {
            return "";
        }
    }

    public enum Choice {
        _ERR_NONE_SELECTED(-1),
        CONNECT_REQUEST(2),
        GET_REQUEST(5),
        SET_REQUEST(6),
        ACTION_REQUEST(7),
        PROXY_REQUEST(9),
        SECURITY_REQUEST(16),
        GET_RESPONSE(133),
        SET_RESPONSE(134),
        CONNECT_RESPONSE(130),
        ACTION_RESPONSE(135),
        REPORT_NOTIFICATION(136),
        PROXY_RESPONSE(137),
        SECURITY_RESPONSE(144);

        private final int value;

        private Choice(int value) {
            this.value = value;
        }

        public int getValue() {
            return value;
        }

        public static Choice getEnumByValue(int value) {
            for (Choice item : Choice.values()) {
                if (value == item.getValue()) {
                    return item;
                }
            }
            return null;
        }
    }
}



